package gui.GLListeners.GLCirculos;

import figuras.Circulo;
import figuras.Figura;
import figuras.PuntoVector;
import gui.GLListeners.GLListener2D;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLJPanel;
import javax.swing.event.MouseInputListener;

import utils.UtilPoligono;
import aplicacion.Vista;
import dataType.DataFlowers;

public class Circulos extends GLListener2D
	implements GLEventListener, MouseInputListener, Observer{
	
	protected GLJPanel lienzo;
	protected DataFlowers componentes;
	protected Vista vista;

	//Atributos propios de los  circulos
	protected Circulo circulo1;
	protected Circulo circulo2;
	protected Circulo circulo3;
	protected Circulo circulo4;
		
	protected double pointsX;
		
	public Circulos(GLJPanel panel, Vista v){
		super();
	
		this.lienzo = panel;
		this.vista = v;
		this.componentes = vista.getControl().getDataFlowers();	
		
		//creamos los circulos
		creaCirculos();
		pointsX = -circulo1.getRadio() - 20;
	}
		
	protected Figura creaCuadrado(Circulo c){
		 
		 double y = c.getRadio();
	     Figura f = new Figura(4);
	     f.ponVertices(0, pointsX, y+2, 0);
	     f.ponVertices(1, pointsX-5, y+2, 0);
	     f.ponVertices(2, pointsX-5, y-2, 0);
	     f.ponVertices(3, pointsX, y-2, 0);
	     return f;
	}
	
	private void creaCirculos() {
		circulo1 = new Circulo(50,componentes.getRadio(0),0,0,0);
        circulo2 = new Circulo(50,componentes.getRadio(1),0,0,0);
        circulo3 = new Circulo(50,componentes.getRadio(2),0,0,0);
        circulo4 = new Circulo(50,componentes.getRadio(3),0,0,0);
	}

	//@Override
	public void display(GLAutoDrawable drw) {
		GL gl = drw.getGL();
		
        gl.glClear(GL.GL_COLOR_BUFFER_BIT);
        
        gl.glBegin(GL.GL_POINT);
        	gl.glVertex2d(0, 0);
        gl.glEnd();  
		
		//pintamos los elementos de la flor
		pintaElementos(0, gl, new Color(1,0,0));
		pintaElementos(1, gl, new Color(0,1,0));
		pintaElementos(2, gl, new Color(0,0,1));
		pintaElementos(3, gl, new Color(1,0,1));
	
	}

	private void pintaElementos(int n, GL gl, Color color) {
		
		ArrayList<PuntoVector> poligono = componentes.getElementos2D(n);
		int size = componentes.getTamanno(n) + 3;
		
		//modificar
		/*gl.glColor3i(componentes.getColor(n).getRed(), 
				componentes.getColor(n).getGreen(), 
				componentes.getColor(n).getBlue());*/
		
		for(int i=0; i<poligono.size();i++){
			
			PuntoVector point = poligono.get(i);
			gl.glTranslated(point.getX(), point.getY(), point.getZ());
			PuntoVector[] aux = UtilPoligono.poligonoRegularXY(new PuntoVector(0,40,0,1), size, n+3, true, gl, color);
			gl.glTranslated(-point.getX(), -point.getY(), -point.getZ());
		}	
	}
			
	public GLJPanel getLienzo() {
		return lienzo;
	}

	public void setLienzo(GLJPanel lienzo) {
		this.lienzo = lienzo;
	}
	
	@Override
	public void update(Observable arg0, Object arg1) {
		if(arg0 == this.componentes)
			lienzo.repaint();
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseDragged(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
